import Vue from "vue";
import Router from "vue-router";
import store from '../store/index';

Vue.use(Router);

const router = new Router({
  routes: [{
      path: "/",
      redirect: "/login"
    },
    {
      path: "/summary",
      meta: {
        title: '汇总',
      },
      component: () => import("@/views/summary/index")
    },
    {
      path: '/login',
      component: () => import('@/views/login/Login'),
      meta: {
        title: '登陆'
      }
    },
    {
      path: '/index',
      component: () => import('@/views/index/index'),
      meta: {
        title: '首页'
      }
    },
    {
      path: "/my",
      component: () => import("@/views/my/my"),
      meta: {
        title: '我的'
      }
    }
  ]
});


router.beforeEach((to, from, next) => {
  let {
    title,
    needLogin
  } = to.meta;
  let {
    isLogin
  } = store.state;
  document.title = title;

  if (needLogin && !isLogin) {
    next({
      path: '/login'
    })
  } else {
    next();
  }
})

export default router;